package cn.hl.hm.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.vizhuo.client.base.AbstractReturnCodeConstant;
import com.vizhuo.client.business.smscode.reply.PostSMSCodeReply;
import com.vizhuo.client.business.smscode.request.PostSMSCodeRequest;
import com.vizhuo.client.business.smscode.request.PostSMSMessageRequest;
import com.vizhuo.mobile.base.AbstractServerController;

import cn.hl.hm.service.SMSCodeIService;
import cn.hl.hm.util.InfoCode;
import cn.hl.hm.util.Result;

/**
 * @author YuanWenLong 作者 E-mail: yuanwenlong2010@163.com
 * @date 创建时间：2015年7月7日 下午5:55:30
 * @version 1.0
 * @parameter
 * @since
 * @return
 */
@Controller
@RequestMapping("sMSCodeController")
public class SMSCodeController extends AbstractServerController {

    private static Logger logger = LoggerFactory.getLogger(SMSCodeController.class);

    @Autowired
    SMSCodeIService sMSCodeIService;
	public void setsMSCodeIService(SMSCodeIService sMSCodeIService) {
		this.sMSCodeIService = sMSCodeIService;
	}

	// 获得验证码，并且存在数据库
    @RequestMapping("getSMSCode")
    public String getCode(
    		@RequestParam("userAccount") String userAccount, 
    		@RequestParam("type") String type,
    		//1:注册2:登录3:找回密码4:司机绑定支付宝5：司机绑银行卡

    	HttpServletRequest request, HttpServletResponse response ) {
    	logger.info("通用---发送短信验证码---sMSCodeController/getSMSCode");
    	PostSMSCodeRequest param = new PostSMSCodeRequest();
    	param.setMobile(userAccount);
    	param.setType(type);
        long serialNumber = 0;
        String rJson = "";
        try {
            PostSMSCodeReply reply = sMSCodeIService.addSMSCode(param);
    		try {
        		Result.ok(reply,InfoCode.SUCCESS, response);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
            
        } catch (Exception e) {
            PostSMSCodeReply reply = new PostSMSCodeReply();
            logger.error("GetSerialNumber=" + serialNumber, e);
            reply.setStateCode(AbstractReturnCodeConstant.SYS_ERROR_UNKNOWN);
            reply.setStateMessage(e.getMessage());
    		Result.ok(reply,InfoCode.FAILURE, response);
    		try {
				response.getWriter().print(rJson);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
        }
        return null;
    }

    // 给推广人发送短信消息
    @RequestMapping("postMessage")
    public String postMessage(HttpServletRequest request, HttpServletResponse response) {
        long serialNumber = 0;
        String rJson = "";
        try {
            PostSMSMessageRequest req = getRequestFromHttpRequest(request, PostSMSMessageRequest.class);
            PostSMSCodeReply reply = null;//sMSCodeIService.postSMSMessage(req);
            rJson = "{\"state\":{\"stateCode\":0,\"stateMessage\":\"发送短信验证码成功！\"},\"data\":{}}";
            //returnJsonResult(response, rJson);
    		try {
    			response.getWriter().print(rJson);
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
            
        } catch (Exception e) {
            PostSMSCodeReply reply = new PostSMSCodeReply();
            logger.error("GetSerialNumber=" + serialNumber, e);
            reply.setStateCode(AbstractReturnCodeConstant.SYS_ERROR_UNKNOWN);
            reply.setStateMessage(e.getMessage());
            //returnJsonResult(response, reply);
            
            rJson = "{\"state\":{\"stateCode\":1,\"stateMessage\":\"发送短信验证码失败！\"},\"data\":{}}";
            //returnJsonResult(response, rJson);
    		try {
				response.getWriter().print(rJson);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
        }
        return null;
    }
}
